﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Services.Gateway.Runtime;

namespace Resequencer.Server {
    class Program {
        static void Main(string[] args) {
            ESB.Authenticate("rpgmaker", "password1");

            if (!Topic.Exists<Resequence>())
                Topic.Register<Resequence>();


            var groupID = "Resequencer";
            var counter = 0;
            var rand = new Random();

            var messages1 = new List<Resequence> { Resequence.New(2), Resequence.New(3), Resequence.New(1), Resequence.New(5), Resequence.New(4),
                Resequence.New(6)};
            var messages2 = new List<Resequence> { Resequence.New(5), Resequence.New(6), Resequence.New(4), 
                Resequence.New(3), Resequence.New(1), Resequence.New(2) };
            var message3 = new List<Resequence> { Resequence.New(1) };
            var messages = new List<Resequence>[] { messages1, messages2, message3 };


            Console.WriteLine("Press enter send unordered message for resequencing. Enter exit to close");

            while (true) {
                var text = Console.ReadLine().ToLower();
                if (text == "exit") break;
                else {
                    var index = rand.Next(1);
                    var msgs = messages[0];
                    foreach (var msg in msgs) {
                        Topic.Select<Resequence>()
                            .ResequenceWith(groupID, msg.ID + counter)
                            .Publish(msg);
                    }
                    counter += msgs.Count;
                }
            }

        }
    }
}
